package com.company.cloud.club.modules.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.company.cloud.club.modules.constants.FileType;
import com.company.cloud.club.modules.core.dto.Result;
import com.company.cloud.club.modules.core.hander.HideResponse;
import com.company.cloud.club.modules.db.domain.FileInfo;
import com.company.cloud.club.modules.dto.IdQuery;
import com.company.cloud.club.modules.dto.LayTableResult;
import com.company.cloud.club.modules.service.FileInfoService;
import com.company.cloud.club.modules.utils.LoginContextUtils;
import com.company.cloud.common.auth.annotation.Login;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.validation.Valid;
import java.util.List;

/**
 * *****************************************************************************
 * <p>
 * 功能名           ：
 * 系统名           ：
 * <p>
 * *****************************************************************************
 * Modification History
 * <p>
 * Date        Name                    Reason for Change
 * ----------  ----------------------  -----------------------------------------
 * 2020/6/12     zhangxu                 Created
 */
@Slf4j
@RestController
@Api(tags = "音视频资源模块API")
@RequestMapping("/file")
public class FileInfoController {

    @Autowired
    private FileInfoService fileInfoService;

    @GetMapping(value = "image/list")
    public LayTableResult getImageListLayTable(@Valid @ModelAttribute FileInfo params) {
        try {
            Integer currentPage = params.getCurrentPage();
            Integer pageSize = params.getPageSize();
            params.setType(FileType.IMAGE);
            IPage<FileInfo> page = fileInfoService.page(new Page<>(currentPage, pageSize), new QueryWrapper<>(params));
            int total = fileInfoService.count(new QueryWrapper<>(params));
            List<FileInfo> records = page.getRecords();
            for (FileInfo info : records) {
                info.setResUrl(info.getUrl());
            }
            return new LayTableResult<>(200, "获取图片列表", records, total);
        } catch (Exception e) {
            log.error("获取图片LayTable列表：%s", e.getMessage(), e);
            return new LayTableResult<>(500, "列表获取失败");
        }
    }

    @GetMapping(value = "image/list/all")
    public Result getImageListAll(@Valid @ModelAttribute FileInfo params) {
        try {
            params.setType(FileType.IMAGE);
            List<FileInfo> list = fileInfoService.list(new QueryWrapper<>(params));
            for (FileInfo info : list) {
                info.setResUrl(info.getUrl());
            }
            return Result.ok(list);
        } catch (Exception e) {
            log.error("绘本获取图片列表：%s", e.getMessage(), e);
            return Result.failed();
        }
    }

    @GetMapping(value = "audio/list")
    @HideResponse
    public Object getAudioListLayTable(@Valid @ModelAttribute FileInfo params) {
        try {
            Integer currentPage = params.getCurrentPage();
            Integer pageSize = params.getPageSize();
            params.setType(FileType.AUDIO);
            IPage<FileInfo> page = fileInfoService.page(new Page<>(currentPage, pageSize), new QueryWrapper<>(params));
            int total = fileInfoService.count(new QueryWrapper<>(params));
            List<FileInfo> records = page.getRecords();
            for (FileInfo info : records) {
                info.setResUrl(info.getUrl());
            }
            return new LayTableResult<>(200, "获取音频列表", records, total);
        } catch (Exception e) {
            log.error("获取音频LayTable列表：%s", e.getMessage(), e);
            return Result.failed();
        }
    }

    @PostMapping(value = "upload/image")
    public Result uploadImage(@RequestParam("file") MultipartFile file) {
        Integer currentUserId = LoginContextUtils.getUserId();
        String url = fileInfoService.uploadImage(currentUserId, file);
        return Result.ok(url);
    }

    @PostMapping(value = "upload/audio")
    public Result uploadAudio(@RequestParam("file") MultipartFile file) {
        Integer currentUserId = LoginContextUtils.getUserId();

        String url = fileInfoService.uploadAudio(currentUserId, file);
        return Result.ok(url);
    }

    @PostMapping(value = "upload/video")
    public Result uploadVideo(@RequestParam("file") MultipartFile file) {
        Integer currentUserId = LoginContextUtils.getUserId();

        String url = fileInfoService.uploadVideo(currentUserId, file);
        return Result.ok(url);
    }

    @PostMapping(value = "upload/wgt")
    public Result uploadWgt(@RequestParam("file") MultipartFile file) {
        Integer currentUserId = LoginContextUtils.getUserId();

        String url = fileInfoService.uploadWgt(currentUserId, file);
        return Result.ok(url);
    }

    @PostMapping(value = "create")
    public Result create(@RequestBody FileInfo params) {
        fileInfoService.save(params);
        return Result.ok(params.getId());
    }

    @PostMapping(value = "update")
    public Result update(@RequestBody FileInfo params) {
        fileInfoService.updateById(params);
        return Result.ok(params);
    }

    @PostMapping(value = "img/delete")
    public Result imgDelete(@RequestBody IdQuery bo) {
        boolean flag = fileInfoService.removeById(bo.getId());
        return Result.ok(flag);
    }

    @GetMapping(value = "detail")
    @HideResponse
    public Result detail(@Valid @ModelAttribute IdQuery bo) {
        FileInfo fileInfo = fileInfoService.getById(bo.getId());
        fileInfo.setResUrl(fileInfo.getUrl());
        return Result.ok(fileInfo);
    }

}
